/*
 * Copyright 2009 Sven Meier
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.malex.mxtracker.component.content;

import com.malex.mxtracker.bean.Project;
import com.malex.mxtracker.util.IAction;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.model.IModel;

import wickettree.AbstractTree;
import wickettree.ITreeProvider;
import wickettree.content.Folder;

/**
 * @author Sven Meier
 */
public class SelectableFolderContent extends Content {

    private static final long serialVersionUID = 1L;

    private IAction<Project> onSelectAction;

    private ITreeProvider<Project> provider;

    private IModel<Project> selected;


    public SelectableFolderContent(ITreeProvider<Project> provider,
                                   IAction<Project> onSelect,
                                   IModel<Project> selected) {
        this.provider = provider;
        this.onSelectAction = onSelect;
        this.selected = selected;
    }

    public void detach() {
        if (selected != null) {
            selected.detach();
        }
    }

    protected boolean isSelected(Project Project) {
        IModel<Project> model = provider.model(Project);

        try {
            return selected != null && selected.equals(model);
        } finally {
            model.detach();
        }
    }

    protected void select(Project project, AbstractTree<Project> tree, final AjaxRequestTarget target) {
        if (selected != null) {
            tree.updateNode(selected.getObject(), target);

            selected.detach();
            selected = null;
        }

        selected = provider.model(project);
        tree.updateNode(project, target);

        if (onSelectAction != null) {
            onSelectAction.execute(project);
        }
    }

    @Override
    public Component newContentComponent(String id, final AbstractTree<Project> tree, IModel<Project> model) {
        return new Folder<Project>(id, tree, model) {
            private static final long serialVersionUID = 1L;

            /**
             * Always clickable.
             */
            @Override
            protected boolean isClickable() {
                return true;
            }

            @Override
            protected void onClick(AjaxRequestTarget target) {
                SelectableFolderContent.this.select(getModelObject(), tree, target);
            }

            @Override
            protected boolean isSelected() {
                Project project = getModelObject();
                return SelectableFolderContent.this.isSelected(project);
            }
        };
    }
}